clear all
cd "D:\Taylor_JAERE_2020_DONE"
use TxnDATA_53stores

*)Drop transactions without a transaction duration
	drop if txn_mins == .	

*) Create Store-by-Week variables
	sort store_id promo_week_id
	egen store_week_group = group(store_id promo_week_id)

*) Bring in store demographics
	merge m:1 store_id using store_demog.dta
	keep if _merge == 3
	drop _merge

*) Create bag variables	
	gen reus_dum = (reus_bag > 0)
		
*) Create total register variables
	gen total_open = cashreg_open + sc_open
	gen total_count = cashreg_count + sc_count	

*) Save for Summary Statistics dofile
	save TxnDATA_53stores_SumStat, replace	

*) Drop extra variables	
	drop txn_id txn_time min remodel_date open_date building_size ///
		selling_size st_bakery st_cust_serve st_pharm st_deli st_floral st_coffee st_gas  ///
		st_juice st_sandwich MedInc HHSizeAverage white black asian over65 useVehicle noVehicle ///
		 
*******************************		
**Creat Event Study Variables**
*******************************

gen enacted = 999999

egen week_group = group(promo_week_id)

												  
	replace enacted = 53 if ( (store_id ==	997 | store_id ==	1476 | store_id ==	1483 | store_id == 987))														  
	replace enacted = 65 if ( (store_id == 640 | store_id == 786))		
	replace enacted = 79 if (store_id ==	2264)
	replace enacted = 85 if (store_id ==	2327)	
	replace enacted = 93 if ((store_id ==	2306 |  store_id ==	2314 |  store_id ==	2560 | store_id ==	2312 | store_id ==	2317))																						 								
	replace enacted = 106 if ( (store_id == 309 | store_id == 669 | store_id == 676 | ///
							store_id ==	790 | store_id ==	908 | ///
							store_id ==	971 | store_id ==	1224 | ///
							store_id ==	1953 | store_id ==	2089))		
	replace enacted = 107 if store_id == 1583									
	replace enacted = 122 if ( (store_id == 999 | store_id ==	1108 | store_id ==	1138))		
	replace enacted = 128 if ( (store_id == 970))		
	replace enacted = 145 if ( (store_id == 747))			
	replace enacted = 157 if (store_id == 2212)			
	replace enacted = 158 if ((store_id == 788 | store_id ==	2078 | store_id ==	4021))			
	replace enacted = 163 if ((store_id == 1245))

gen E=(week_group==enacted)	
tab store_id if E==1	
gen E_diff = week_group-enacted
tab E_diff

gen Ecap0=(E_diff<=-24 & E_diff > -999)
gen pw23=(E_diff==-23)
gen pw22=(E_diff==-22)
gen pw21=(E_diff==-21)
gen pw20=(E_diff==-20)
gen pw19=(E_diff==-19)
gen pw18=(E_diff==-18)
gen pw17=(E_diff==-17)
gen pw16=(E_diff==-16)
gen pw15=(E_diff==-15)
gen pw14=(E_diff==-14)
gen pw13=(E_diff==-13)
gen pw12=(E_diff==-12)
gen pw11=(E_diff==-11)
gen pw10=(E_diff==-10)
gen pw9=(E_diff==-9)
gen pw8=(E_diff==-8)
gen pw7=(E_diff==-7)
gen pw6=(E_diff==-6)
gen pw5=(E_diff==-5)
gen pw4=(E_diff==-4)
gen pw3=(E_diff==-3)
gen pw2=(E_diff==-2)
gen pw1=(E_diff==-1)

gen fw1=(E_diff==1)
gen fw2=(E_diff==2)
gen fw3=(E_diff==3)
gen fw4=(E_diff==4)
gen fw5=(E_diff==5)
gen fw6=(E_diff==6)
gen fw7=(E_diff==7)
gen fw8=(E_diff==8)
gen fw9=(E_diff==9)
gen fw10=(E_diff==10)
gen fw11=(E_diff==11)
gen fw12=(E_diff==12)
gen fw13=(E_diff==13)
gen fw14=(E_diff==14)
gen fw15=(E_diff==15)
gen fw16=(E_diff==16)
gen fw17=(E_diff==17)
gen fw18=(E_diff==18)
gen fw19=(E_diff==19)
gen fw20=(E_diff==20)
gen fw21=(E_diff==21)
gen fw22=(E_diff==22)
gen fw23=(E_diff==23)
gen Ecap1=(E_diff>=24)

replace POST = 0 if E_diff<0

save TxnDATA_53stores_forReg.dta, replace		

			
*********************************************************************************************************************
										******************************* 
										* (STORE BY WEEK) Main Analyses
										*******************************
*********************************************************************************************************************

		clear all
		use TxnDATA_53stores_forReg	
		
		collapse (mean) txn_mins paper_bag reus_dum tot_scan expenditure_paid DEPT_* ///
						(max) POST pw* fw* Ecap* E promo_week_id total_open, by(store_id store_city week_group)	
													
		gen ln_mins = ln(txn_mins)
					
		egen city_clust = group(store_city)

*) Main event study, without controls 
		reghdfe ln_mins Ecap0 pw23-pw2 o.pw1 E fw1-fw23 Ecap1, absorb(store_id week_group) vce(cluster city_clust week_group)		
			eststo REG1					

		* test equivalence *
		test E = fw1 = fw2 = fw3 = fw4 = fw5 = fw6 = fw7 = fw8 = fw9 = fw10 = fw11 = fw12 = fw13 = fw14 = fw15 = fw16 = fw17 = fw18 = fw19 = fw20 =fw21 = fw22 = fw23 = Ecap1	
		test fw1 = E 
		test fw2 = E 
		test fw3 = E
		test fw4 = E
		test fw5 = E 
		test fw6 = E
		test fw7 = E 
		test fw8 = E 
		test fw9 = E
		test fw10 = E
		test fw11 = E 
		test fw12 = E 
		test fw13 = E 
		test fw14 = E 
		test fw15 = E 
		test fw16 = E 
		test fw17 = E 
		test fw18 = E 
		test fw19 = E 
		test fw20 = E 
		test fw21 = E 
		test fw22 = E 
		test fw23 = E 		
		test Ecap1 = E 
		
		* Figure 2 *		
		coefplot 	(REG1, m(circle) mc(blue) mlc(blue) ciopts(lc(blue*.5))), omitted ///
		vert yline(0,  lcolor(gs5))  xline(24)  levels(95) keep(Ecap0 pw* E fw* Ecap1) xsize(5) ysize(2) /// 
		xlabel( 1 "<-24" 5 "-20" 9 "-16" 13 "-12" 17 "-8" 21 "-4" 25 "0" 29 "4" 33 "8" 37 "12" 41 "16" 45 "20" 49 "≥24", labsize(large)) ///
		scheme(s1mono) xtitle(Weeks Since DCB Policy, size(vlarge)) ytitle(% Diff. in Txn. Duration, size(vlarge)) legend(off) ///
		ylabel(-0.05 "-5.0%" -0.025 "-2.5%" 0 0.025 "2.5%"0.05 "5.0%" 0.075 "7.5%" 0.1 "10%" , angle(0) labsize(large)) 
				graph export "Fig2_MainResult.pdf", as(pdf) replace
				
		* Appendix Table A.2 *		
		esttab REG1 using "${latex}A2Table.tex", ///
		mtitles("All") align(c) s(N r2 se storefix datefix, ///
		label("Num of Obs." "R squared" "Standard Errors" "Store FE" "Date FE")) ///
		replace b(%12.3f) se compress nonotes  ///
		sfmt(%6.0s %10.3f %10.0f %10.3f) star(* 0.10 ** 0.05 *** 0.01) noobs label 
		
*) Main event study, with controls		
		reghdfe ln_mins Ecap0 pw23-pw2 o.pw1 E fw1-fw23 Ecap1 tot_scan expenditure_paid ///
						DEPT_*, absorb(store_id week_group)  vce(cluster city_clust week_group)	
		eststo REG1
		
		* Appendix Figure A.2 *
		coefplot 	(REG1, m(circle) mc(blue) mlc(blue) ciopts(lc(blue*.5))), omitted ///
		vert yline(0,  lcolor(gs5))  xline(24)  levels(95) keep(Ecap0 pw* E fw* Ecap1) xsize(5) ysize(2) /// 
		xlabel( 1 "<-24" 5 "-20" 9 "-16" 13 "-12" 17 "-8" 21 "-4" 25 "0" 29 "4" 33 "8" 37 "12" 41 "16" 45 "20" 49 "≥24", labsize(large)) ///
		scheme(s1mono) xtitle(Weeks Since DCB Policy, size(vlarge)) ytitle(% Diff. in Txn. Duration, size(vlarge)) legend(off) ///
		ylabel(-0.05 "-5.0%" -0.025 "-2.5%" 0 0.025 "2.5%"0.05 "5.0%" 0.075 "7.5%" 0.1 "10%" , angle(0) labsize(large)) 
				graph export "AppFig2_MainResultControl.pdf", as(pdf) replace
						
*) Main diff-in-diff		
		reghdfe ln_mins POST , absorb(store_id week_group) vce(cluster city_clust week_group)		
				eststo REG2	
		reghdfe txn_mins POST , absorb(store_id week_group) vce(cluster city_clust week_group)		
				eststo REG3	
				
				
		* Table 3 & 5 *
		esttab REG2 REG3 using "${latex}did_txnmins.tex", ///
		mtitles("Store-Week") ///
		align(c) ///
		s(N r2 se storefix datefix, ///
		label("Num of Obs." "R squared" "Standard Errors" "Store FE" "Date FE")) ///
		replace b(%12.3f) se compress nonotes  ///
		sfmt(%6.0s %10.3f %10.0f %10.3f) star(* 0.10 ** 0.05 *** 0.01) noobs label 
		sum txn_mins if promo_week_id < 201201
		
*) Transaction Share by bag type		
		reghdfe paper_bag Ecap0 pw23-pw2 o.pw1 E fw1-fw23 Ecap1, absorb(store_id week_group) vce(cluster city_clust week_group)		
		eststo Paper
						
		reghdfe reus_dum Ecap0 pw23-pw2 o.pw1 E fw1-fw23 Ecap1, absorb(store_id week_group) vce(cluster city_clust week_group)		
		eststo Reusable
			
		* Figure 4a *	
		coefplot (Paper, m(circle) mc(cranberry) mlc(cranberry) ciopts(lc(cranberry*.5))) ///
				(Reusable, m(circle_hollow) mc(blue) mlc(blue) ciopts(lc(blue*.5))), omitted  ///
		vert yline(0,  lcolor(gs5))  xline(24)  levels(95) keep(Ecap0 pw* E fw* Ecap1) xsize(5) ysize(2) /// 
		xlabel( 1 "<-24" 5 "-20" 9 "-16" 13 "-12" 17 "-8" 21 "-4" 25 "0" 29 "4" 33 "8" 37 "12" 41 "16" 45 "20" 49 "≥24", labsize(large)) ///
		 scheme(s1mono) xtitle(Weeks Since DCB Policy, size(vlarge)) ylabel(-0.05(0.05)0.25, angle(0) labsize(large)) ///
		ytitle(Share of Txns. Purchasing, size(vlarge)) legend(label(1 "Paper" 2 "No Paper")) legend(region(lc(none))) legend(size(large))
				graph export "Fig4a_ShareBag.pdf", as(pdf) replace
			

 *) Policy effect on number and types of items purchased (table 4)
			reghdfe tot_scan POST, absorb(store_id week_group) vce(cluster city_clust week_group)		
			estadd local storefix "Yes", replace 
			estadd local datefix "Yes", replace
			estadd loc se "Cluster"
			estimates store REG1
			
			reghdfe expenditure_paid POST, absorb(store_id week_group) vce(cluster city_clust week_group)		
			estadd local storefix "Yes", replace 
			estadd local datefix "Yes", replace
			estadd loc se "Cluster"
			estimates store REG2
		
		* Table 4 *
		esttab REG1 REG2 using "${latex}table4a.tex", ///
		mtitles("Items per Txn." "Expenditure per Txn." "Expenditure per Item") ///
		align(cccccc) keep(POST) ///
		s(N r2 se storefix datefix, ///
		label("Num of Obs." "R squared" "Standard Errors" "Store FE" "Date FE")) ///
		replace b(%12.3f) se compress nonotes  ///
		sfmt(%6.0s %10.3f %10.0f %10.3f) star(* 0.10 ** 0.05 *** 0.01) noobs label 				
		sum tot_scan expenditure_paid if promo_week_id < 201201
		
			
			*) Alcohol & Tobacco
			reghdfe DEPT_1 POST, absorb(store_id week_group) vce(cluster city_clust week_group)	
			estadd local storefix "Yes", replace 
			estadd local datefix "Yes", replace
			estadd loc se "Cluster"
			estimates store REG1	
			
			*) Bakery & Deli
			reghdfe DEPT_2 POST, absorb(store_id week_group) vce(cluster city_clust week_group)
			estadd local storefix "Yes", replace 
			estadd local datefix "Yes", replace
			estadd loc se "Cluster"
			estimates store REG2		

			*) Dairy and Refrig
			reghdfe DEPT_3 POST, absorb(store_id week_group) vce(cluster city_clust week_group)
			estadd local storefix "Yes", replace 
			estadd local datefix "Yes", replace
			estadd loc se "Cluster"
			estimates store REG3	
			
			*) Floral
			reghdfe DEPT_4 POST, absorb(store_id week_group) vce(cluster city_clust week_group)	
			estadd local storefix "Yes", replace 
			estadd local datefix "Yes", replace
			estadd loc se "Cluster"
			estimates store REG4	
			
			*) Frozen
			reghdfe DEPT_6 POST, absorb(store_id week_group) vce(cluster city_clust week_group)
			estadd local storefix "Yes", replace 
			estadd local datefix "Yes", replace
			estadd loc se "Cluster"
			estimates store REG6	
			
			*) Meat & Seafood
			reghdfe DEPT_7 POST, absorb(store_id week_group) vce(cluster city_clust week_group)	
			estadd local storefix "Yes", replace 
			estadd local datefix "Yes", replace
			estadd loc se "Cluster"
			estimates store REG7	
			
			*) Produce			
			reghdfe DEPT_9 POST, absorb(store_id week_group) vce(cluster city_clust week_group)
			estadd local storefix "Yes", replace 
			estadd local datefix "Yes", replace
			estadd loc se "Cluster"
			estimates store REG9	
			
			*) Pet
			reghdfe DEPT_10 POST, absorb(store_id week_group) vce(cluster city_clust week_group)
			estadd local storefix "Yes", replace 
			estadd local datefix "Yes", replace
			estadd loc se "Cluster"
			estimates store REG10	
			
			*) Other Grocery
			reghdfe DEPT_11 POST, absorb(store_id week_group) vce(cluster city_clust week_group)
			estadd local storefix "Yes", replace 
			estadd local datefix "Yes", replace
			estadd loc se "Cluster"
			estimates store REG11	
			
			*) Baby		
			reghdfe DEPT_12 POST, absorb(store_id week_group) vce(cluster city_clust week_group)
			estadd local storefix "Yes", replace 
			estadd local datefix "Yes", replace
			estadd loc se "Cluster"
			estimates store REG12				
				
		esttab REG9 REG7 REG3 REG6 REG2 using "${latex}table4b.tex", ///
		mtitles("Produce" "Meat & Seafood" "Dairy & Refrigerated" "Frozen" "Bakery & Deli" ) ///
		align(cccccc) keep(POST) ///
		s(N r2 se storefix datefix, ///
		label("Num of Obs." "R squared" "Standard Errors" "Store FE" "Date FE")) ///
		replace b(%12.3f) se compress nonotes  ///
		sfmt(%6.0s %10.3f %10.0f %10.3f) star(* 0.10 ** 0.05 *** 0.01) noobs label 
		
		esttab REG11 REG1 REG12 REG4 REG10 using "${latex}table4c.tex", ///
		mtitles("Shelf-Stable Food" "Alcohol & Tobacco" "Baby Items" "Floral" "Pet") ///
		align(cccccc) keep(POST) ///
		s(N r2 se storefix datefix, ///
		label("Num of Obs." "R squared" "Standard Errors" "Store FE" "Date FE")) ///
		replace b(%12.3f) se compress nonotes  ///
		sfmt(%6.0s %10.3f %10.0f %10.3f) star(* 0.10 ** 0.05 *** 0.01) noobs label 		
		sum DEPT_* if promo_week_id < 201201

*) Policy effect on number of registers open 
	reghdfe total_open POST , absorb(store_id week_group) vce(cluster city_clust week_group)	
			estadd local storefix "Yes", replace 
			estadd local datefix "Yes", replace
			estadd loc se "Cluster"
			estimates store REG1		

		* Table 4 *			
		esttab REG1 using "${latex}table4d.tex", ///
		mtitles("Open registers") ///
		align(ccc) keep(POST) ///
		s(N r2 se storefix datefix, ///
		label("Num of Obs." "R squared" "Standard Errors" "Store FE" "Date FE")) ///
		replace b(%12.3f) se compress nonotes  ///
		sfmt(%6.0s %10.3f %10.0f %10.3f) star(* 0.10 ** 0.05 *** 0.01) noobs label 
			sum total_open if promo_week_id < 201201


*********************************************************************************************************************
										**************************************** 
										* (STORE BY WEEK) Transactions per shift 
										****************************************
*********************************************************************************************************************

		clear all		
		use TxnDATA_53stores_forReg		
				
		collapse (mean) store_day_TRANS (max) POST pw* fw* Ecap* E promo_week_id, ///
						by(store_id store_city week_group txn_date)
		
		
		collapse (mean) store_day_TRANS (max) POST pw* fw* Ecap* E promo_week_id, ///
						by(store_id store_city week_group)
		
		gen ln_store_day_TRANS = ln(store_day_TRANS)		
	
		egen city_clust = group(store_city)

*) Policy effect on the number of transaction per three-hour-shift

	reghdfe store_day_TRANS POST , absorb(store_id week_group) vce(cluster city_clust week_group)	
		estadd local storefix "Yes", replace 
		estadd local datefix "Yes", replace
		estadd loc se "Cluster"
		estimates store REG1a
		
	reghdfe ln_store_day_TRANS POST, absorb(store_id week_group) vce(cluster city_clust week_group)	
		estadd local storefix "Yes", replace 
		estadd local datefix "Yes", replace
		estadd loc se "Cluster"
		estimates store REG1b		
	
	* Table 3 *
	esttab REG1a REG1b using "${latex}did_txnday.tex", ///
	mtitles("All" "All" ) ///
	align(cccccc) keep(POST) ///
	s(N r2 se storefix datefix, ///
	label("Num of Obs." "R squared" "Standard Errors" "Store FE" "Date FE")) ///
	replace b(%12.3f) se compress nonotes  ///
	sfmt(%6.0s %10.3f %10.0f %10.3f) star(* 0.10 ** 0.05 *** 0.01) noobs label 			
	sum store_day_TRANS if promo_week_id < 201201
	
	
		

	
*********************************************************************************************************************
										******************************* 
										* (STORE BY WEEK BY Paper Bag Use)
										*******************************
*********************************************************************************************************************

		clear all	
		use TxnDATA_53stores_forReg
		

		collapse (mean) txn_mins tot_scan expenditure_paid DEPT_* (max) POST pw* fw* Ecap* E promo_week_id, ///
						by(store_id store_city week_group paper_bag)
									
		gen ln_mins = ln(txn_mins)
			
		egen city_clust = group(store_city)		

*) Policy effects by paper bag use	
		reghdfe ln_mins Ecap0 pw23-pw2 o.pw1 E fw1-fw23 Ecap1 tot_scan expenditure_paid DEPT_* if paper_bag != 1 ///
								, absorb(store_id week_group) vce(cluster city_clust week_group)			
			eststo NoPaper
 
 			
		reghdfe ln_mins Ecap0 pw23-pw2 o.pw1 E fw1-fw23 Ecap1 tot_scan expenditure_paid DEPT_* if (paper_bag != 0 | POST != 1) ///
								, absorb(store_id week_group) vce(cluster city_clust week_group)			
			eststo Paper
		test E = fw1 = fw2 = fw3 = fw4 = fw5 = fw6 = fw7 = fw8 = fw9 = fw10 = fw11 = fw12 = fw13 = fw14 = fw15 = fw16 = fw17 = fw18 = fw19 = fw20 =fw21 = fw22 = fw23 = Ecap1	
		test fw1 = E 
		test fw2 = E 
		test fw3 = E
		test fw4 = E
		test fw5 = E 
		test fw6 = E
		test fw7 = E 
		test fw8 = E 
		test fw9 = E
		test fw10 = E
		test fw11 = E 
		test fw12 = E 
		test fw13 = E 
		test fw14 = E 
		test fw15 = E 
		test fw16 = E 
		test fw17 = E 
		test fw18 = E 
		test fw19 = E 
		test fw20 = E 
		test fw21 = E 
		test fw22 = E 
		test fw23 = E 		
		test Ecap1 = E
		
		* Figure 4b *
		coefplot 	(Paper, m(circle) mc(cranberry) mlc(cranberry) ciopts(lc(cranberry*.5))) ///
			(NoPaper, m(circle_hollow) mc(blue) mlc(blue) ciopts(lc(blue*.5))), omitted  ///
		vert yline(0,  lcolor(gs5))  xline(24)  levels(95) keep(Ecap0 pw* E fw* Ecap1) xsize(5) ysize(2) /// 
		xlabel( 1 "<-24" 5 "-20" 9 "-16" 13 "-12" 17 "-8" 21 "-4" 25 "0" 29 "4" 33 "8" 37 "12" 41 "16" 45 "20" 49 "≥24", labsize(large)) ///
		 scheme(s1mono) xtitle(Weeks Since DCB Policy, size(vlarge)) ///
		ytitle(% Diff. in Txn. Duration, size(vlarge)) legend(label(1 "Paper" 2 "No Paper")) legend(size(large)) legend(region(lc(none))) ///
		ylabel(-0.1 "-10%" -0.05 "-5%" 0 0.05 "5%" 0.1 "10%" 0.15 "15%" , angle(0) labsize(vlarge)) 
				graph export "Fig4b_PaperBagUse.pdf", as(pdf) replace

	
*********************************************************************************************************************
										******************************* 
										* (STORE BY WEEK BY SC REGISTER) 
										*******************************
*********************************************************************************************************************

		clear all	
		use TxnDATA_53stores_forReg
		
		bysort store_id week_group: egen txn_mins_tot = mean(txn_mins)
		bysort store_id week_group sc: egen txn_mins_sc = mean(txn_mins)
		bysort store_id week_group: egen share_sc = mean(sc)
		
		collapse (mean) txn_mins txn_mins_tot txn_mins_sc share_sc (max) POST pw* fw* Ecap* E promo_week_id, ///
						by(store_id store_city week_group sc st_has_self_check)
									
		gen ln_mins = ln(txn_mins)
		gen ln_mins_tot = ln(txn_mins_tot)
		gen ln_mins_sc = ln(txn_mins_sc)		
			
		egen city_clust = group(store_city)
		
*) Policy effect by Register type
		reghdfe ln_mins_sc POST if sc == 0 & st_has_self_check == 1, absorb(store_id week_group) vce(cluster store_id week_group)	
			eststo CashierReg
			
		reghdfe ln_mins_sc POST if sc == 1 & st_has_self_check == 1, absorb(store_id week_group) vce(cluster store_id week_group)	 
			eststo SCReg
	
		reghdfe ln_mins_tot POST if sc == 0 & st_has_self_check == 0, absorb(store_id week_group) vce(cluster store_id week_group)	 
			eststo NoSCStore
		
		reghdfe ln_mins_tot POST if sc == 0 & st_has_self_check == 1, absorb(store_id week_group) vce(cluster store_id week_group)	 
			eststo SCStore

		* Figure 5 *	
		coefplot    (NoSCStore, rename(POST = "No SC stores") m(circle) mc(cranberry) mlc(cranberry) ciopts(lc(cranberry*.5)) msize(large)) ///
					(SCStore, rename(POST = "SC stores") m(circle) mc(blue) mlc(blue) ciopts(lc(blue*.5))msize(large)) ///
					(CashierReg, rename(POST = "Cashiers at SC stores") m(circle_hollow) mc(cranberry) mlc(cranberry) ciopts(lc(cranberry*.5))msize(large)) ///
					(SCReg, rename(POST = `""SC at SC stores""') m(circle_hollow) mc(blue) mlc(blue) ciopts(lc(blue*.5))msize(large)),  ///
		 yline(0) vertical xsize(5) ysize(2) levels(95) scheme(s1mono)  nooffsets /// 
		 keep(POST)	ytitle(% Diff. in Txn. Duration, size(vlarge))  ///
		ylabel(-0.05 "-5%" 0 0.05 "5%" 0.1 "10%" , angle(0) labsize(large)) xlabel(, labsize(vlarge)) legend(off)			
				graph export "Fig5_SelfCheckResults.pdf", as(pdf) replace		

*) Share using SC at stores with self-checkout				
			reghdfe share_sc Ecap0 pw23-pw3 o.pw2 pw1 E fw1-fw23 Ecap1 if st_has_self_check == 1 & sc==0, ///
						absorb(store_id week_group) vce(cluster city_clust week_group)		
			eststo SCShare			
			
		* Appendix Figure A.4 * 
		coefplot (SCShare, m(circle) mc(blue) mlc(blue) ciopts(lc(blue*.5))), omitted ///
		vert yline(0,  lcolor(gs5))  xline(24)  levels(95) keep(Ecap0 pw* E fw* Ecap1) xsize(5) ysize(2) /// 
		xlabel( 1 "<-24" 5 "-20" 9 "-16" 13 "-12" 17 "-8" 21 "-4" 25 "0" 29 "4" 33 "8" 37 "12" 41 "16" 45 "20" 49 "≥24", labsize(large)) ///
		 scheme(s1mono) xtitle(Weeks Since DCB Policy, size(vlarge)) ///
		ytitle(% Diff. in Txn. Duration, size(vlarge)) legend(off) ///
		ylabel(-0.05 "-5.0%" -0.025 "-2.5%" 0 0.025 "2.5%"0.05 "5.0%" , angle(0) labsize(large)) 
				graph export "SCShare.pdf", as(pdf) replace


			
		
*********************************************************************************************************************
										******************************* 
										* (CASHIER BY WEEK) 
										*******************************
*********************************************************************************************************************

clear all	
use TxnDATA_53stores_forReg	

*) Keep only cashier-operated lanes
	keep if reg_nbr < 21

*) Drop cashiers that aren't human or are managers	
	drop if cashier_nbr < 10000
			
*) Make a variable for store-cashier 
	egen store_cashier = group (store_id cashier_nbr)
						
	collapse (mean) txn_mins (max) POST pw* fw* Ecap* E promo_week_id, ///
				by(store_id store_city week_group cashier_nbr store_cashier)					
				
*) Keep cashiers that we see atleast 17 weeks 
	duplicates tag store_cashier, gen(CashierRegDUP)
	tab CashierRegDUP
	sum CashierRegDUP, detail 
	drop if CashierRegDUP < 17 
	sum CashierRegDUP, detail 

*) Cashier SUMMARY STATS
	preserve
	collapse (mean) CashierRegDUP, by(store_cashier)
	sum CashierRegDUP, detail
	restore					
	
	egen tag_store_cashier = tag(store_cashier)	
	bysort store_id: egen cashier_per_store = sum(tag_store_cashier)	

	preserve
	collapse (mean) cashier_per_store, by(store_id)
	sum cashier_per_store, detail
	restore	

	gen ln_mins = ln(txn_mins)
		
	egen city_clust = group(store_city)

*) Cashier-week analysis
	
		reghdfe ln_mins POST, absorb(store_id week_group store_cashier) vce(cluster city_clust week_group)		
			estadd local storefix "Yes", replace 
			estadd local datefix "Yes", replace
			estadd loc se "Cluster"
			estimates store REG1	
			
		esttab REG1 using "${latex}table5b.tex", ///
		mtitles("Cashier-Week" ) ///
		align(cccccc) keep(POST) ///
		s(N r2 se storefix datefix, ///
		label("Num of Obs." "R squared" "Standard Errors" "Store FE" "Date FE")) ///
		replace b(%12.3f) se compress nonotes  ///
		sfmt(%6.0s %10.3f %10.0f %10.3f) star(* 0.10 ** 0.05 *** 0.01) noobs label 
		
		
*********************************************************************************************************************
										******************************* 
										* (TRANSACTION LEVEL) 
										*******************************
*********************************************************************************************************************
		
		clear all	
		use TxnDATA_53stores_forReg
		
		gen ln_mins = ln(txn_mins+0.01)
		egen city_clust = group(store_city)

*) Policy effect at the transaction level	
	reghdfe ln_mins POST , absorb(store_id date_group hh_id#store_id cashier_nbr#store_id reg_nbr#store_id) vce(cluster city_clust date_group) 
			estadd local storefix "Yes", replace 
			estadd local datefix "Yes", replace
			estadd loc se "Cluster"
			estimates store REG1	
		
		* Table 5 *
		esttab REG1 using "${latex}table5d.tex", ///
		mtitles("Transaction Level" ) ///
		align(cccccc) keep(POST) ///
		s(N r2 se storefix datefix, ///
		label("Num of Obs." "R squared" "Standard Errors" "Store FE" "Date FE")) ///
		replace b(%12.3f) se compress nonotes  ///
		sfmt(%6.0s %10.3f %10.0f %10.3f) star(* 0.10 ** 0.05 *** 0.01) noobs label 
			
*) Policy effect by number of items in a transaction		
eststo clear

	matrix b = (.,.,.,.)
	forvalues xx = 1(1)75  {
		display `xx'
		qui reghdfe txn_mins POST if tot_scan == `xx', absorb(store_id week_group) vce(cluster city_clust week_group)		
		matrix x = r(table)
		matrix b = b\(`xx',x[1,1],x[5,1],x[6,1])
		mat drop x
	}	
		qui reghdfe txn_mins POST if tot_scan > 75, absorb(store_id week_group) vce(cluster city_clust week_group)		
		matrix x = r(table)
		matrix b = b\(76,x[1,1],x[5,1],x[6,1])
		mat drop x

	clear
	svmat b
	drop if b1 == .
	rename b1 bw 
	rename b2 beta
	rename b3 lb
	rename b4 ub
	twoway (scatter beta bw, m(circle) mc(blue) mlc(blue) m(circle_hollow)) (rcap lb ub bw, lpattern(solid) m(circle_hollow)lcolor(blue) msize(0)) , xlabel(0(5)75) yline(0.059, lcolor(red) lpattern(dash)) ///
	 title("") scheme(s1mono) ylabel(-0.1 0 0.1 0.2 0.3 0.4 0.5 0.6 , labsize(large) angle(0)) yline(0) ///
	 xtitle("Number of Items Scanned", size(medium)) legend(off) ytitle("Diff. in Txn. Duration (min)", size(large)) legend(label(1 "95% CI") label(2 "Est. Coefficient"))
	graph export "FigA4_HetByItems.pdf", replace
	clear

	
*********************************************************************************************************************
										***************************************************
										* (STORE BY WEEK) EVENT STUDY -- Extended Endpoints
										***************************************************
*********************************************************************************************************************
	
	clear all	
	use TxnDATA_53stores_forReg

		gen Ecap0_yr=(E_diff<=-49 & E_diff > -999)

		gen pw48=(E_diff==-48)
		gen pw47=(E_diff==-47)
		gen pw46=(E_diff==-46)
		gen pw45=(E_diff==-45)
		gen pw44=(E_diff==-44)
		gen pw43=(E_diff==-43)
		gen pw42=(E_diff==-42)
		gen pw41=(E_diff==-41)
		gen pw40=(E_diff==-40)
		gen pw39=(E_diff==-39)
		gen pw38=(E_diff==-38)
		gen pw37=(E_diff==-37)
		gen pw36=(E_diff==-36)
		gen pw35=(E_diff==-35)
		gen pw34=(E_diff==-34)
		gen pw33=(E_diff==-33)
		gen pw32=(E_diff==-32)

		gen pw31=(E_diff==-31)
		gen pw30=(E_diff==-30)
		gen pw29=(E_diff==-29)
		gen pw28=(E_diff==-28)
		gen pw27=(E_diff==-27)
		gen pw26=(E_diff==-26)
		gen pw25=(E_diff==-25)
		gen pw24=(E_diff==-24)

		gen fw24=(E_diff==24)
		gen fw25=(E_diff==25)
		gen fw26=(E_diff==26)
		gen fw27=(E_diff==27)
		gen fw28=(E_diff==28)
		gen fw29=(E_diff==29)
		gen fw30=(E_diff==30)
		gen fw31=(E_diff==31)
		gen fw32=(E_diff==32)
		gen fw33=(E_diff==33)

		gen fw34=(E_diff==34)
		gen fw35=(E_diff==35)
		gen fw36=(E_diff==36)
		gen fw37=(E_diff==37)
		gen fw38=(E_diff==38)
		gen fw39=(E_diff==39)
		gen fw40=(E_diff==40)
		gen fw41=(E_diff==41)
		gen fw42=(E_diff==42)
		gen fw43=(E_diff==43)
		gen fw44=(E_diff==44)
		gen fw45=(E_diff==45)
		gen fw46=(E_diff==46)
		gen fw47=(E_diff==47)
		gen fw48=(E_diff==48)

		gen Ecap1_yr=(E_diff>=49)

		gen fw49=(E_diff==49)
		gen fw50=(E_diff==50)
		gen fw51=(E_diff==51)
		gen fw52=(E_diff==52)
		gen fw53=(E_diff==53)
		gen fw54=(E_diff==54)
		gen fw55=(E_diff==55)
		gen fw56=(E_diff==56)
		gen fw57=(E_diff==57)
		gen fw58=(E_diff==58)
		gen fw59=(E_diff==59)
		gen fw60=(E_diff==60)
		gen fw61=(E_diff==61)
		gen fw62=(E_diff==62)
		gen fw63=(E_diff==63)
		gen fw64=(E_diff==64)
		gen fw65=(E_diff==65)
		gen fw66=(E_diff==66)
		gen fw67=(E_diff==67)
		gen fw68=(E_diff==68)
		gen fw69=(E_diff==69)
		gen fw70=(E_diff==70)
		gen fw71=(E_diff==71)
		gen fw72=(E_diff==72)
		gen fw73=(E_diff==73)
		gen fw74=(E_diff==74)
		gen fw75=(E_diff==75)
		gen fw76=(E_diff==76)
		gen fw77=(E_diff==77)
		gen fw78=(E_diff==78)
		gen fw79=(E_diff==79)
		gen fw80=(E_diff==80)
		gen fw81=(E_diff==81)
		gen fw82=(E_diff==82)
		gen fw83=(E_diff==83)
		gen fw84=(E_diff==84)
		gen fw85=(E_diff==85)
		gen fw86=(E_diff==86)
		gen fw87=(E_diff==87)
		gen fw88=(E_diff==88)
		gen fw89=(E_diff==89)
		gen fw90=(E_diff==90)
		gen fw91=(E_diff==91)
		gen fw92=(E_diff==92)
		gen fw93=(E_diff==93)
		gen fw94=(E_diff==94)
		gen fw95=(E_diff==95)
		gen fw96=(E_diff==96)

		gen Ecap1_2yr=(E_diff>=97)

		order pw23-pw1, after(pw24)
		
		collapse (mean) txn_mins (max) POST pw* fw* Ecap* E promo_week_id, ///
						by(store_id store_city week_group)	
									
		gen ln_mins = ln(txn_mins)
		
		egen city_clust = group(store_city)
			
	
*) Extended endpoint event-study specification

		reghdfe ln_mins Ecap0 pw23-pw2 o.pw1 E fw1-fw23 Ecap1  ///
								, absorb(store_id week_group) vce(cluster city_clust week_group)	
			eststo mainRes_short						

					
		
		reghdfe ln_mins Ecap0_yr pw48-pw2 o.pw1 E fw1-fw96 Ecap1_2yr  ///
								, absorb(store_id week_group) vce(cluster city_clust week_group)	
			eststo mainRes_2yr						
			
		coefplot (mainRes_2yr, m(circle) mc(white) mlc(blue) ciopts(lc(blue*.5)))  ///
					(mainRes_short, m(circle) mc(blue) mlc(blue) ciopts(lc(blue*.5))), omitted ///
		vert yline(0,  lcolor(gs5))  xline(49)  levels(95) keep(Ecap0_yr pw* E fw* Ecap1_2yr) xsize(6) ysize(2) /// 
		xlabel( 1 "<-48" 10 "-40" 18 "-32" 26 "-24" 34 "-16" 42 "-8" 50 "0" 58 "8" 66 "16" 74 "24" 82 "32" 90 "40" 98 "48" 106 "56" 114 "64" 122 "72" 130 "80" 138 "88" 147 ">96", labsize(large)) ///
		xline(26,  lcolor(gs5) lw(medium) lp(dash)) xline(74,  lcolor(gs5) lw(medium) lp(dash)) scheme(s1mono) xtitle(Weeks Since DCB Policy, size(vlarge)) ///
		ytitle(% Diff. in Txn. Duration, size(vlarge)) ///
		ylabel(-0.1 "-10%" -0.05 "-5%" 0 0.05 "5%" 0.1 "10%" 0.15 "15%", angle(0) labsize(large)) ///
		legend(off) saving(eventStudy, replace)
		
		graph bar (sum) pw48-pw2 pw1 E fw1-fw96, legend(off) scheme(s1mono) xsize(6) ysize(1) fysize(15) ///
		ytitle("# of Treated " "Stores", size(vlarge)) outergap(0) bargap(150) ///
		ylabel(0(15)30, labsize(large) angle(0)) saving(storeBar, replace) ///
		inten(10000) linten(10000)

		gr combine eventStudy.gph storeBar.gph, col(1) imargin(0 0 0 0) scheme(s1mono) xcom iscale(*.75)
		graph export "Fig3_ExtendedEndpoints.pdf", as(pdf) replace		
		
		
*********************************************************************************************************************
										**********************************
										* (Customer BY Month) EVENT STUDY 
										**********************************
*********************************************************************************************************************	

clear all
use TxnDATA_53stores_SumStat

*) Drop extra variables	
	drop txn_id txn_time min remodel_date open_date building_size ///
		selling_size st_bakery st_cust_serve st_pharm st_deli st_floral st_coffee  ///
		st_gas st_juice st_sandwich MedInc HHSizeAverage white black asian over65 useVehicle ///
		noVehicle noBan 
		
*) Drop customer cards most likely belonging to staff or stores 		
	duplicates tag hh_id store_id, gen(hh_dup)
	sum hh_dup, detail
	preserve 
	duplicates drop hh_id hh_dup, force
	sum hh_dup, detail
	tab hh_dup
	restore
	drop if hh_dup > 100

*) Generate household and month-of-sample groups	
	egen hh_group = group(store_id hh_id)	
	egen month_group = group(year month)

*) Create event study variables
	gen enacted = 999999

	replace enacted = 13 if ((store_id ==	997 | store_id ==	1476 | store_id ==	1483 | store_id == 987))	
	replace enacted = 16 if ( (store_id == 640 | store_id == 786))			
	replace enacted = 19 if (store_id ==	2264)
	replace enacted = 20 if (store_id ==	2327)															
	replace enacted = 22 if (store_id==2306|store_id==2314|store_id==2560|store_id==2312|store_id==2317)																														  
	replace enacted = 25 if (store_id == 309 | store_id == 669 | store_id == 676 | ///
							store_id ==	790 | store_id ==	908 | ///
							store_id ==	971 | store_id ==	1224 | ///
							store_id ==	1583 | store_id ==	1953 | store_id ==	2089)			
	replace enacted = 29 if ( (store_id ==999 | store_id ==1108 | store_id ==1138))		
	replace enacted = 30 if ( (store_id == 970))		
	replace enacted = 34 if ( (store_id == 747))			
	replace enacted = 37 if (store_id == 2212)				
	replace enacted = 37 if ((store_id == 788 | store_id ==	2078 | store_id ==	4021))			
	replace enacted = 38 if ((store_id == 1245))
	
	gen E=(month_group==enacted)	
	tab store_id if E==1	
	gen E_diff = month_group-enacted
	tab E_diff

	gen Ecap0=(E_diff<=-8 & E_diff > -999)
	gen pw7=(E_diff==-7)
	gen pw6=(E_diff==-6)
	gen pw5=(E_diff==-5)
	gen pw4=(E_diff==-4)
	gen pw3=(E_diff==-3)
	gen pw2=(E_diff==-2)
	gen pw1=(E_diff==-1)

	gen fw1=(E_diff==1)
	gen fw2=(E_diff==2)
	gen fw3=(E_diff==3)
	gen fw4=(E_diff==4)
	gen fw5=(E_diff==5)
	gen Ecap1=(E_diff>=6)

*) Collapse to the customer-month	
	collapse (mean) txn_mins paper_bag (max) POST pw* fw* Ecap* E, ///
			by(store_id store_city year month TREAT hh_group month_group)	
					
	sort hh_group month_group
	bysort hh_group: gen monthsFromStart = _n
	
	sort hh_group month_group
	bysort hh_group POST: gen monthsFromBan = _n
	replace monthsFromBan = 0 if POST == 0		

	gen ln_mins = ln(txn_mins)	

		duplicates tag hh_group, gen(hh_dup)
		tab hh_dup
		keep if hh_dup > 10
		
		preserve 
		duplicates drop hh_group, force
		sum hh_dup, detail
		restore
		
		egen city_clust = group(store_city)	
	
*) Ever/Never Purchased Paper	
		preserve	
		keep if paper_bag >0
		duplicates drop hh_group store_id, force
		keep hh_group
		gen paper_HH = 1
		save paper_HHs.dta, replace
		restore
	
	merge m:1 hh_group using paper_HHs.dta	
	replace paper_HH = 0 if paper_HH ==.
	drop _merge	

*) Always Purchased Paper	
		preserve	
		duplicates tag hh_group paper_bag, gen(toDrop)
		drop if toDrop > 0
		drop toDrop
		keep if paper_bag >0		
		keep hh_group
		gen paper_always_HH = 1
		duplicates drop hh_group, force		
		save paper_always_HHs.dta, replace
		restore
	
	merge m:1 hh_group using paper_always_HHs.dta	
	replace paper_always_HH = 0 if paper_always_HH ==.
	drop _merge	

*) Policy effect (customer-month)
		tab monthsFromStart, gen(WFS_)
		tab monthsFromBan, gen(WFB_)	
		
		reghdfe ln_mins POST, absorb(store_id month_group hh_group) vce(cluster city_clust month_group)		
			estadd local storefix "Yes", replace 
			estadd local datefix "Yes", replace
			estadd loc se "Cluster"
			estimates store REG1	
			
		cd "D:\Dropbox\17_UCBARE2\JMP\TABLES"
		esttab REG1 using "${latex}table5c.tex", ///
		mtitles("Household-Month" ) ///
		align(cccccc) keep(POST) ///
		s(N r2 se storefix datefix, ///
		label("Num of Obs." "R squared" "Standard Errors" "Store FE" "Date FE")) ///
		replace b(%12.3f) se compress nonotes  ///
		sfmt(%6.0s %10.3f %10.0f %10.3f) star(* 0.10 ** 0.05 *** 0.01) noobs label 
		
		
	reghdfe ln_mins Ecap0 pw7-pw3 pw2 o.pw1 E fw1-fw5 Ecap1  ///
		, absorb(store_id month_group hh_group) vce(cluster city_clust month_group)		
		eststo REGA
		
		* Figure A3 *		
		coefplot (REGA, m(circle) mc(blue) mlc(blue) ciopts(lc(blue*.5))), omitted ///
		vert yline(0,  lcolor(gs5))  xline(8)  levels(95) keep(Ecap0 pw* E fw* Ecap1) xsize(4) ysize(2) /// 
		xlabel( 1 "≤-8" 2 "-7" 3 "-6" 4 "-5" 5 "-4" 6 "-3" 7 "-2" 8 "-1" 9 "0" 10 "1" 11 "2" 12 "3" 13 "4" 14 "5" 15 "≥6", labsize(large)) ///
		scheme(s1mono) xtitle(Weeks Since DCB Policy, size(vlarge)) ytitle(% Diff. in Txn. Duration, size(vlarge)) legend(off) ///
		ylabel(-0.05 "-5.0%" -0.025 "-2.5%" 0 0.025 "2.5%"0.05 "5.0%" 0.075 "7.5%" 0.1 "10%" , angle(0) labsize(large)) 
				cd "D:\Dropbox\17_UCBARE2\JMP\FIGURES\"
				graph export "FigA3a_Customer.pdf", as(pdf) replace

			
	*) Treated Paper Ever
	preserve
	keep if (paper_HH == 1 & TREAT ==1) | TREAT ==0  
		reghdfe ln_mins Ecap0 pw7-pw3 pw2 o.pw1 E fw1-fw5 Ecap1, absorb(store_id month_group hh_group) vce(cluster city_clust month_group) 	
		eststo Paper	
		
	*) Treated No Paper Ever
	restore
	preserve
	keep if (paper_HH == 0  & TREAT ==1) | TREAT ==0  
		reghdfe ln_mins Ecap0 pw7-pw3 pw2 o.pw1 E fw1-fw5 Ecap1, absorb(store_id month_group hh_group monthsFromStart) vce(cluster city_clust month_group) 	
		eststo NoPaper			
	restore
			
		coefplot (Paper, m(circle) mc(cranberry) mlc(cranberry) ciopts(lc(cranberry*.5))) ///
				(NoPaper, m(circle_hollow) mc(blue) mlc(blue) ciopts(lc(blue*.5))), omitted ///
		vert yline(0,  lcolor(gs5))  xline(8.175)  levels(95) keep(Ecap0 pw* E fw* Ecap1) xsize(4) ysize(2) /// 
		xlabel( 1 "≤-8" 2 "-7" 3 "-6" 4 "-5" 5 "-4" 6 "-3" 7 "-2" 8 "-1" 9 "0" 10 "1" 11 "2" 12 "3" 13 "4" 14 "5" 15 "≥6", labsize(large)) ///
		 scheme(s1mono) xtitle(Months Since DCB Policy, size(vlarge)) ///
		ytitle(% Diff. in Txn. Duration, size(vlarge)) legend(label(1 "Paper" 2 "No Paper")) legend(region(lc(none))) ///
		ylabel(-0.05 "-5.0%" -0.025 "-2.5%" 0 0.025 "2.5%"0.05 "5.0%" 0.075 "7.5%" 0.1 "10%" , angle(0) labsize(large))  
				cd "D:\Dropbox\17_UCBARE2\JMP\FIGURES\"
				graph export "FigA3b_Customer.pdf", as(pdf) replace			
		